<?php

namespace app\common\logic;
use addons\third\model\Third;
use think\Db;
use think\exception\ValidateException;


/**
 * 单位逻辑曾
 */
class LearnLogic
{
   //获取当前单位所属的顶级单位方法
    private $page = 1;
    private $pagesize = 15;
    protected $cate = null;
    public function __construct()
    {
        $this->cate = config('course');
    }


    /*
     * 家长课程
     * $learn_cate_id 分类id  代表那个分类的课程
     * $type 为小学 初中  高中 。。。
     */
    public function getLearnCateCourse1($learn_cate_id = 0, $type = 0, $pagesize=0){
        $where = array();
        $group = 0;
        if($learn_cate_id){
            $where['learn_cate_id'] = $learn_cate_id;
            if($learn_cate_id == 4){//家长必修课
                $third= Third::get(UID);
                if(isset($third['u_id']) && $third['u_id'] > 0){
                    $org_grade_id = Db::name('org_grade_user')->where(['user_id'=>$third['u_id']])->value('org_grade_id');
                    $group = Db::name('org_grade')->where(['id'=>$org_grade_id])->value('group');
                    $where['grade'] = $group;
                }else{
                    $where['grade'] = '-1';
                }
            }
        }
        if($type){
            $where['type'] = $type;
        }

        $where['status'] = 0;
//        $where['org_cate_id'] = ORG_CATE_ID;
        if(ORG_TYPE  == 3){//单位类型：1普通单位 2付费单位 3分成单位
            $where['org_cate_id'] = ['in', [0, ORG_CATE_ID]]; //本单位和总平台课程
            $where2 = '1 <>1 ';
        }elseif(ORG_TYPE  == 1){
            $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
            $where2 = '1 <>1 ';
        }else{// 2付费单位
            $where['org_cate_id'] = ORG_CATE_ID;
            $res = Db::name('learn_course_orgpay')->where('org_cate_id', ORG_CATE_ID)->column('course_id');
            if($res){
                $where2 = ' learn_course_id in ('.implode(',', $res).') and status = 0';
                if($type){
                    $where2 .= ' and type = '.$type;
                }
                if($learn_cate_id){
                    $where2 .= ' and learn_cate_id = '.$learn_cate_id;
                    if($learn_cate_id == 4){
                        if($group > 0){
                            $where2 .= ' and grade = '.$group;
                        }else{
                            $where2 .= ' and grade = -1';
                        }
                    }

                }

            }else{
                $where2 = '1 <>1 ';
            }
        }
        if(!$pagesize){
            $pagesize = $this->pagesize;
        }
        //子查询条件
        $map['id'] = ['in', function($query) use ($where, $where2){
            $query->name('learn_cate_course1')->where($where)->whereOrRaw($where2)->field('learn_course_id');
        }];
        $map['is_chat'] = 0;
        $map['status'] = 0;
        $courseLogic = new CourseLogic();
        $list = $courseLogic->getList($map, $pagesize);

        return $list;
    }

    /*
     * 大咖课程
     */
    public function getLearnCateCourse2($learn_cate_teache_id = 0,  $pagesize=0){
        $where = array();

//
//        $where['o.org_cate_id'] = 0;
//        $where['status'] = 0;
//        $where['type'] = 1;
//        $where2 = '1 <>1 ';
        if(ORG_TYPE  == 2){
            if($learn_cate_teache_id){
                $where['c.teacher_id'] = $learn_cate_teache_id;
            }
            $where['o.org_cate_id'] = ORG_CATE_ID;
            $where['c.status'] = 0;
            $where['c.special'] = 2;
//            $where['t.type'] = 1;
            $map['id'] = ['in', function($query) use ($where){
                $query->name('learn_course_orgpay')->alias('o')
                    ->join('edu_learn_course c', 'o.course_id = c.id', 'left')
//                    ->join('edu_learn_cate_teacher t', 'c.teacher_id = t.id', 'left')
                    ->where($where)
//                    ->where('c.special', 2)
//                    ->where('t.type', 1)
                    ->field('ocourse_id');
            }];
//            $res = Db::name('learn_course_orgpay')->alias('o')
//                ->join('edu_learn_course c', 'o.course_id = c.id', 'left')
//                ->join('edu_learn_cate_teacher t', 'c.teacher_id = t.id', 'left')
//                ->where('o.org_cate_id', ORG_CATE_ID)
//                ->where('c.special', 2)
//                ->where('t.type', 1)
//                ->column('ocourse_id');
//            if($res){
//                $where2 = 'learn_course_id in ('.implode(',', $res).')';
//            }else{
//                $where2 = '1 <>1 ';
//            }
        }else{
            if($learn_cate_teache_id){
                $map['teacher_id'] = $learn_cate_teache_id;
            }
//            $where['org_cate_id'] = 0;
//            $where['status'] = 0;
//            $where['type'] = 1;
//            $map['teacher_id'] = ['in', function($query) use ($where){
//                $query->name('learn_cate_teacher')->where($where)->field('teacher_id');
//            }];
        }

        if(!$pagesize){
            $pagesize = $this->pagesize;
        }

        //子查询条件
        $map['is_chat'] = 0;
        $map['special'] = 2;
        $courseLogic = new CourseLogic();
        $list = $courseLogic->getList($map, $pagesize);

        return $list;
    }
    /*
     *   专题分类列表 专题
     */
    public function getLearnCateSpecial($where = [], $limit = null){
        $where['status'] = 0;
//        $where['org_cate_id'] = ORG_CATE_ID;
        $where['learn_cate_id'] = $this->cate['special_course'];
        $model = Db::name('learn_cate_special')
            ->where($where)->order('sort asc')
            ->field(true);
        if ($limit === null) {
            $list = $model->select();
        } else {
            $list = $model->limit($limit)->select();
        }

        return $list;
    }

    /*
     *   专题分类课程列表
     */
    public function getLearnCateSpecialCourse($special_id = 0, $pagesize = null){
        if ($pagesize === null) {
            $pagesize = $this->pagesize;
        }
        $where = array();
        $where['status'] = 0;

        if(ORG_TYPE  == 3){//单位类型：1普通单位 2付费单位 3分成单位
            $where['org_cate_id'] = ['in', [0, ORG_CATE_ID]]; //本单位和总平台课程
            $where2 = '1 <>1 ';
        }elseif(ORG_TYPE  == 1){
            $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
            $where2 = '1 <>1 ';
        }else{// 2付费单位
            $where['org_cate_id'] = ORG_CATE_ID;
            $res = Db::name('learn_course_orgpay')->where('org_cate_id', ORG_CATE_ID)->column('course_id');
            if($res){
                $where2 = 'learn_course_id in ('.implode(',', $res).') and status = 0';
            }else{
                $where2 = '1 <>1 ';
            }
        }

        if($special_id){
            $where['learn_special_id'] = $special_id;
            $where2.=' and learn_special_id = '.$special_id;
        }
        $map['is_chat'] = 0;
        $map['id'] = ['in', function($query) use ($where, $where2){
            $query->name('learn_cate_course3')->where($where)->whereOrRaw($where2)->field('learn_course_id');
        }];
        $courseLogic = new CourseLogic();
        $list = $courseLogic->getList($map, $pagesize);
        return $list;
    }

    //大咖老师 列表
    public function getTeachers($where = [], $limit = null) {
        $where['status'] = 0;
        $where['type'] = 1;
        $where['org_cate_id'] = 0;

        $model = Db::name('learn_cate_teacher')
            ->where($where)->order('sort asc')
            ->field(true);
        if ($limit === null) {
            $list = $model->select();
        } else {
            $list = $model->limit($limit)->select();
        }

        return $list;
    }

    //获取大咖老师 课程
    public function getTeachersCourse($teacher_id= 0, $pagesize = null, $order = null) {
        if ($pagesize === null) {
            $pagesize = $this->pagesize;
        }
        $where = array();
//        $where['status'] = 0;
//        $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
//        if($teacher_id){
//            $where['learn_cate_teache_id'] = $teacher_id;
//        }
//        if(ORG_TYPE  == 3){//单位类型：1普通单位 2付费单位 3分成单位
//            $where['org_cate_id'] = ['in', [0, ORG_CATE_ID]]; //本单位和总平台课程
//            $where2 = '1 <>1 ';
//        }elseif(ORG_TYPE  == 1){
//            $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
//            $where2 = '1 <>1 ';
//        }else{// 2付费单位
//            $where['org_cate_id'] = ORG_CATE_ID;
//            $res = Db::name('learn_course_orgpay')->where('org_cate_id', ORG_CATE_ID)->column('course_id');
//            if($res){
//                $where2 = 'learn_course_id in ('.implode(',', $res).')';
//            }else{
//                $where2 = '1 <> 1 ';
//            }
//        }
//        $map['id'] = ['in', function($query) use ($where,$where2){
//            $query->name('learn_cate_course2')->where($where)->whereOrRaw($where2)->field('learn_course_id');
//        }];
        if(ORG_TYPE  == 2){
            if($teacher_id){
                $where['c.teacher_id'] = $teacher_id;
            }
            $where['o.org_cate_id'] = ORG_CATE_ID;
            $where['c.status'] = 0;
            $where['c.is_chat'] = 0;
            $where['c.special'] = 2;
            $map['id'] = ['in', function($query) use ($where){
                $query->name('learn_course_orgpay')->alias('o')
                    ->join('edu_learn_course c', 'o.course_id = c.id', 'left')
                    ->where($where)
                    ->field('o.course_id');
            }];
        }else{
            if($teacher_id){
                $map['teacher_id'] = $teacher_id;
            }
        }
        $map['is_chat'] = 0;
        $map['special'] = 2;
        $courseLogic = new CourseLogic();
        if ($order != null) {
            $list = $courseLogic->getList($map, $pagesize, $order);
        } else {
            $list = $courseLogic->getList($map, $pagesize);
        }
        return $list;
    }

    //获取热门课程
    public function getHotCourse($pagesize = null) {
        if ($pagesize === null) {
            $pagesize = $this->pagesize;
        }
        $where = array();
        $where['status'] = 0;
//        $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
        if(ORG_TYPE  == 3){//单位类型：1普通单位 2付费单位 3分成单位
            $where['org_cate_id'] = ['in', [0, ORG_CATE_ID]]; //本单位和总平台课程
            $where2 = '1 <>1 ';
        }elseif(ORG_TYPE  == 1){
            $where['org_cate_id'] = ORG_CATE_ID; //本单位课程
            $where2 = '1 <>1 ';
        }else{// 2付费单位
            $where['org_cate_id'] = ORG_CATE_ID;
            $res = Db::name('learn_course_orgpay')->where('org_cate_id', ORG_CATE_ID)->column('course_id');
            if($res){
                $where2 = 'id in ('.implode(',', $res).') and status = 0';
            }else{
                $where2 = '1 <>1 ';
            }
        }
        $map['id'] = ['in', function($query) use ($where,$where2){
            $query->name('learn_course')->where($where)->whereOrRaw($where2)->field('id');
        }];
        $map['is_chat'] = 0;
        $map['is_hot'] = 2;
        $courseLogic = new CourseLogic();
        $list = $courseLogic->getList($map, $pagesize);
        return $list;
    }

}
